﻿using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI.WebControls;
using Thksoft.Modal.Page;

namespace Thksoft.Modal
{
    public partial class EditCompanyBank : FormPage
    {
        private Sys_CompanyBLL companyBLL = new Sys_CompanyBLL();
        private Sys_DepartmentBLL departmentBLL = new Sys_DepartmentBLL();
        private Yx_AccountBLL accountBLL = new Yx_AccountBLL();
        private Sys_BankBLL bankBLL = new Sys_BankBLL();

        protected override void BindPage()
        {
            if (FORM_IFEDIT)
            {
                var id = Convert.ToDecimal(FORM_KEY);
                var entity = accountBLL.Find(Convert.ToDecimal(FORM_KEY));
                FORM_TYPE.SelectedValue = entity.Account_Type.ToString();
                FORM_ACCOUNT_NUMBER.Text = entity.Account_No;
                FORM_REMARK.Text = entity.Remark;
                FORM_STATE.SelectedValue = entity.State.ToString();
                BindCompany(entity.Company_Id);
                BindDepartment(entity.Demartment_Id);
                BinBank(entity.N_BANK_ID);
            }
            else
            {
                BindCompany();
                BindDepartment();
                BinBank();
            }
        }

        #region 绑定数据

        private void BindCompany(decimal? id = null)
        {
            DataTable dtCompany = companyBLL.GetInfoByPId((int)CurrentUserCompanyID);
            FORM_COMPANY_ID.DataSource = dtCompany;
            FORM_COMPANY_ID.DataTextField = "CMY_NAME";
            FORM_COMPANY_ID.DataValueField = "CMY_ID";
            FORM_COMPANY_ID.DataBind();
            FORM_COMPANY_ID.Items.Insert(0, new ListItem("---请选择---", "-1"));
            if (id == null)
            {
                FORM_COMPANY_ID.SelectedValue = "-1";
            }
            else
            {
                FORM_COMPANY_ID.SelectedValue = id.ToString();
            }
        }

        private void BindDepartment(decimal? id = null)
        {
            if (id == null)
            {
                FORM_DEPARTMENT_ID.Items.Insert(0, new ListItem("---请选择---", "-1"));
                FORM_DEPARTMENT_ID.SelectedValue = "-1";
            }
            else
            {
                var dt = departmentBLL.GetInfoALLCmyID(FORM_COMPANY_ID.SelectedValue);

                FORM_DEPARTMENT_ID.DataSource = dt;
                FORM_DEPARTMENT_ID.DataTextField = "DEPTNAME";
                FORM_DEPARTMENT_ID.DataValueField = "DEPTID";
                FORM_DEPARTMENT_ID.DataBind();
                FORM_DEPARTMENT_ID.Items.Insert(0, new ListItem("---请选择---", "-1"));
                FORM_DEPARTMENT_ID.SelectedValue = id.ToString();
            }
        }

        private void BinBank(decimal? id = null)
        {
            int count;
            var dt = bankBLL.GetEntitys(null, null, 0, 1000, out count);

            FORM_BANK_ID.DataSource = dt;
            FORM_BANK_ID.DataTextField = "C_NAME";
            FORM_BANK_ID.DataValueField = "N_ID";
            FORM_BANK_ID.DataBind();
            FORM_BANK_ID.Items.Insert(0, new ListItem("---请选择---", "-1"));
            if (id == null)
            {
                FORM_BANK_ID.SelectedValue = "-1";
            }
            else
            {
                FORM_BANK_ID.SelectedValue = id.ToString();
            }
        }

        protected void FORM_COMPANY_CHANGE(object sender, EventArgs e)
        {
            var dt = departmentBLL.GetInfoALLCmyID(FORM_COMPANY_ID.SelectedValue);

            FORM_DEPARTMENT_ID.DataSource = dt;
            FORM_DEPARTMENT_ID.DataTextField = "DEPTNAME";
            FORM_DEPARTMENT_ID.DataValueField = "DEPTID";
            FORM_DEPARTMENT_ID.DataBind();
            FORM_DEPARTMENT_ID.Items.Insert(0, new ListItem("---请选择---", "-1"));
            FORM_DEPARTMENT_ID.SelectedValue = "-1";
        }

        #endregion 绑定数据

        protected override void SaveClick(object sender, EventArgs e)
        {
            if (FORM_COMPANY_ID.SelectedValue == "-1")
            {
                MessageBox.Show(this, "公司不能选择空");
                return;
            }
            if (FORM_DEPARTMENT_ID.SelectedValue == "-1")
            {
                MessageBox.Show(this, "部门不能选择空");
                return;
            }
            if (FORM_BANK_ID.SelectedValue == "-1")
            {
                MessageBox.Show(this, "银行类型不能选择空");
                return;
            }
            if (FORM_ACCOUNT_NUMBER.Text.Trim() == "")
            {
                MessageBox.Show(this, "银行账号不能为空");
                return;
            }
            if (FORM_IFEDIT)
            {
                var entity = accountBLL.Find(Convert.ToDecimal(FORM_KEY));
                entity.Company_Id = Convert.ToDecimal(FORM_COMPANY_ID.SelectedValue);
                entity.Account_No = FORM_ACCOUNT_NUMBER.Text.Trim();
                entity.Account_Type = Convert.ToDecimal(FORM_TYPE.SelectedValue);
                entity.State = Convert.ToDecimal(FORM_STATE.SelectedValue);
                entity.Operator_Id = CurrentUserID;
                entity.Operator_date = DateTime.Now;
                entity.Remark = FORM_REMARK.Text.TrimEnd();
                entity.Demartment_Id = Convert.ToDecimal(FORM_DEPARTMENT_ID.SelectedValue);
                entity.N_BANK_ID = Convert.ToDecimal(FORM_BANK_ID.SelectedValue);
                //entity.OpenBank=FORM_BANK_ID.SelectedItem.Text;
                //entity.N_BANK_ID = Convert.ToDecimal(FORM_BANK_ID.SelectedItem.Text);
                if (accountBLL.Update(entity, CurrentUserName))
                {
                    MessageBox.Show(this, "修改成功");
                }
                else
                {
                    MessageBox.Show(this, "修改失败");
                }
            }
            else
            {
                var entity = new Yx_Account();
                entity.Company_Id = Convert.ToDecimal(FORM_COMPANY_ID.SelectedValue);
                entity.Account_No = FORM_ACCOUNT_NUMBER.Text.Trim();
                entity.Account_Type = Convert.ToDecimal(FORM_TYPE.SelectedValue);
                entity.State = Convert.ToDecimal(FORM_STATE.SelectedValue);
                entity.Operator_Id = CurrentUserID;
                entity.Operator_date = DateTime.Now;
                entity.Remark = FORM_REMARK.Text.TrimEnd();
                entity.Demartment_Id = Convert.ToDecimal(FORM_DEPARTMENT_ID.SelectedValue);
                entity.N_BANK_ID = Convert.ToDecimal(FORM_BANK_ID.SelectedValue);
                //entity.OpenBank = FORM_BANK_ID.Text;
                if (accountBLL.Insert(entity, CurrentUserName))
                {
                    MessageBox.ShowAndRedirect(this, "新增成功", "/Modal/EditCompanyBank.aspx?IF_EDIT=FALSE");//TRUE&ID="+entity.Account_Id);
                }
                else
                {
                    MessageBox.Show(this, "新增失败");
                }
            }
        }

        protected override void UpdateTitle()
        {
            if (FORM_IFEDIT)
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"编辑公司银行账户\");");
            }
            else
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"添加公司银行账户\");");
            }
        }

        //protected override void UpdateFormSize()
        //{
        //    MessageBox.ResponseScript(this, "FORM_SIZE(900,400);");
        //}
    }
}